package com.cxx.purchasecharge.component.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.cxx.purchasecharge.core.config.BaseConfigUtil;

public class MessageProperties extends Properties
{
    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger (MessageProperties.class);
    private static final String PURCHASE_CHARGE_PROPERTIES_FILE = "pc-component-config.properties";

    private static MessageProperties messageProperties = getInstance ();

    private MessageProperties (String fileName)
    {
        String cxxPropertiesDir = BaseConfigUtil.getCxxPropertiesDir ();
        InputStream inputStream = null;

        if (cxxPropertiesDir != null && !"".equals (cxxPropertiesDir))
        {
            cxxPropertiesDir = cxxPropertiesDir.trim ();
            if (!cxxPropertiesDir.endsWith (File.separator))
            {
                cxxPropertiesDir += File.separator;
            }
            cxxPropertiesDir += fileName;
            logger.info (fileName + " file path is " + cxxPropertiesDir);

            try
            {
                inputStream = new FileInputStream (cxxPropertiesDir);
                load (new InputStreamReader (inputStream, "UTF-8"));
            }
            catch (FileNotFoundException e)
            {
                logger.warn ("Can't find " + fileName, e);
            }
            catch (IOException e)
            {
                logger.warn ("Can't load " + fileName, e);
            }
        }
        else
        {
            logger.warn ("Can't find properties directory.");
        }
    }

    private static synchronized MessageProperties getInstance ()
    {
        if (messageProperties == null)
        {
            messageProperties = new MessageProperties (PURCHASE_CHARGE_PROPERTIES_FILE);
        }
        return messageProperties;
    }

    public static String getConfig (String key)
    {
        return messageProperties.getProperty (key);
    }

    public static String getConfig (String key, String defaultValue)
    {
        String value = defaultValue;
        try
        {
            value = messageProperties.getProperty (key);
        }
        catch (Exception e)
        {
            logger.warn ("The " + key + " not exist", e);
        }
        return value;
    }

    public static String getConfigFormatted (String key, Object... arguments)
    {
        String value = "";
        try
        {
            String pattern = messageProperties.getProperty (key);
            value = MessageFormat.format (pattern, arguments);
        }
        catch (Exception e)
        {
            logger.warn ("The " + key + " getConfigFormatted not exist", e);
        }
        return value;
    }

}
